_xrnconv-cli_autocomplete() {
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    #prev="${COMP_WORDS[COMP_CWORD-1]}"
    command=$( echo "${COMP_WORDS[@]}" | tr -s ' ' '\n' | awk '/--/{print $0}' )
    opts_std2xrn="-source -msbs -lsbs -metadata"
    opts_xrn2std="-msbs -lsbs -metadata -destination"

    # Convert space-separated strings to arrays
    read -r -a array0 <<< "$opts_std2xrn"
    read -r -a array1 <<< "${COMP_WORDS[@]}"

    # Initialize an empty array to store the result
    result=()

    # Loop over each element in the first array
    for item in "${array0[@]}"; do
        # Check if the element exists in the second array
        if [[ ! " ${array1[@]} " =~ " $item " ]]; then
            # Add the element to the result array
            result+=("$item")
       fi
    done
    opts_std2xrn_sub="${result[*]}"

    # Convert space-separated strings to arrays
    read -r -a array0 <<< "$opts_xrn2std"
    read -r -a array1 <<< "${COMP_WORDS[@]}"

    # Initialize an empty array to store the result
    result=()

    # Loop over each element in the first array
    for item in "${array0[@]}"; do
        # Check if the element exists in the second array
        if [[ ! " ${array1[@]} " =~ " $item " ]]; then
            # Add the element to the result array
            result+=("$item")
       fi
    done
    opts_xrn2std_sub="${result[*]}"


    # Check if an option has already been used
    local used_opts=$(echo "${COMP_WORDS[@]}" | grep -oE -- '(\s|^)--[a-zA-Z0-9]+' | sort | uniq)

    case "${command}" in
        --std2xrn)
            COMPREPLY=( $(compgen -W "${opts_std2xrn_sub}" -- ${cur}) )
            return 0
            ;;
        --xrn2std)
            COMPREPLY=( $(compgen -W "${opts_xrn2std_sub}" -- ${cur}) )
            return 0
            ;;
        --help)
            COMPREPLY=( $(compgen -W "" -- ${cur}) )
            return 0
            ;;
        *)
            COMPREPLY=( $(compgen -W "--help --std2xrn --xrn2std $(echo "${opts}" | grep -v -E "${used_opts}")" -- ${cur}) )
            return 0
            ;;
    esac
}
complete -F _xrnconv-cli_autocomplete xrnconv-cli

